$(function(){
	var codelength=$('#courtname').width();
	$('#courtname').validatebox({
	    required: true,
	    validType: {maxLength:[50]},
	    tipPosition:'top',
	    deltaX:codelength/4
	});
	$('#vadhead').validatebox({
	    required: true,
	    validType: {between:[0,1800000]},
	    tipPosition:'top',
	    deltaX:codelength/4
	});
	$('#vadseg').validatebox({
	    required: true,
	    validType: {between:[0,30000]},
	    tipPosition:'top',
	    deltaX:codelength/4
	});
	$('#maxsecond').validatebox({
	    required: true,
	    validType: {between:[0,60]},
	    tipPosition:'top',
	    deltaX:codelength/4
	});
});
/**
 * 添加麦克风
 */
function addMicro(){
	var index=$("#microList").children('div').length+1;
	var microHtml='<div style="padding-top:10px">'+
		'<input placeholder="麦克风名称"  type="text" class="form-control" name="drCourt.maxsecond" id="maxsecond" style="width:100px;margin-left:0px" />'+
		'<input placeholder="麦克风地址" type="text" class="form-control" name="drCourt.maxsecond" id="maxsecond" style="width:210px;margin-left:5px" />'+
		'<img src="'+delUrl+'" title="删除麦克风" class="titleimg" onclick="delMicro('+index+')" id="delMi'+index+'">'+
		'</div>';
	$("#microList").append(microHtml);
	$("#microList").scrollTop($("#microList")[0].scrollHeight);
}
/**
 * 删除麦克风
 */
function delMicro(index){
	$("#delMi"+index).parent().remove();
}
/**
 * 扫描麦克风
 */
function syncMicro(){
	var microUrl=$("#remoteurl").val();
	if(microUrl==''){
		parent.$.messager.alert("失败","请输入麦克风地址",'error');
		return false;
	}
	window.parent.showloading();
	$.ajax({
		type:'get',
		url:$Path+'/admin/micro/getMicroPhone',
		data:{url:microUrl},
		success:function(data){
			window.parent.hideloading();
			if(data.code==200){
				var href=data.results.hurl;
				var result=data.results.microCodes;
				$("#microList").children('div').remove();
				for(var i=0;i<result.length;i++){
					var index=$("#microList").children('div').length+1;
					var microHtml='<div style="padding-top:10px">'+
						'<input placeholder="麦克风名称" value="'+result[i].name+'"  type="text" class="form-control" name="microName" style="width:100px;margin-left:0px" />'+
						'<input placeholder="麦克风地址" value="'+href+result[i].code+'" type="text" class="form-control" name="microCode" style="width:210px;margin-left:5px" />'+
						'<img src="'+delUrl+'" title="删除麦克风" class="titleimg" onclick="delMicro('+index+')" id="delMi'+index+'">'+
						'</div>';
					$("#microList").append(microHtml);
				}
			}
			else{
				parent.$.messager.alert("失败","同步失败："+data.msg,'error');
			}
		},error:function(){
			window.parent.hideloading();
			parent.$.messager.alert("失败","服务器开小差了~",'error');
		}
	})
}


function subBtn(){
	var phones=[];
	var childs=$("#microList").children('div');
	for(var i=0;i<childs.length;i++){
		var phone={};
		var childInput=childs[i].children;
		if(childInput==''||childInput==undefined||childInput.leng<=0){
			continue;
		}
		phone.microname=childInput[0].value;
		if(phone.microname==''||phone.microname==undefined){
			parent.$.messager.alert("失败","第"+(i+1)+"个麦克风名称不可为空",'error');
			return false;
		}
		phone.microcode=childInput[1].value;
		if(phone.microcode==''||phone.microcode==undefined){
			parent.$.messager.alert("失败","第"+(i+1)+"个麦克风地址不可为空",'error');
			return false;
		}
		phones.push(phone);
	}
	var drCourt={};
	if(!validate()){
		return false;
	}
	drCourt.courtid=$("#courtid").val();
	drCourt.courtname=$("#courtname").val();
	drCourt.vadhead=$("#vadhead").val();
	drCourt.vadseg=$("#vadseg").val();
	drCourt.maxsecond=$("#maxsecond").val();
	drCourt.remoteurl=$("#remoteurl").val();
	var auth=window.parent.getAuth();
	window.parent.showloading();
	$.ajax({
		type:'post',
		url:$Path+'/admin/court/saveCourt',
		data:{drCourt:drCourt,phones:phones,authenticityToken:auth},
		success:function(data){
			window.parent.hideloading();
			if(data.code==200){
//				parent.parent.$.messager.alert("成功","同步成功<br />法庭名称:"+$("#courtname").val()+"<br />麦克风数量:"+jdata.msg,'info',function(){
					window.parent.refreshTab("法庭管理")
					parent.$("#dlg").window("close");
//				});
			}else{
				parent.$.messager.alert("失败",data.msg,'error');
			}
		},error:function(){
			window.parent.hideloading();
			parent.$.messager.alert("失败","服务器开小差了~",'error');
		}
	})
}

function validate(){
	var courtname=$("#courtname").val();
	var vadhead=$("#vadhead").val();
	var vadseg=$("#vadseg").val();
	var maxsecond=$("#maxsecond").val();
	if(courtname==''||courtname==undefined){
		parent.$.messager.alert("失败","法庭名称不可为空",'error');
		return false;
	}
	if(courtname.length>50){
		parent.$.messager.alert("失败","法庭名称长度超出限制",'error');
		return false;
	}
	if(vadhead==''||vadhead==undefined){
		parent.$.messager.alert("失败","首部静音不可为空",'error');
		return false;
	}
	if(vadhead<0||vadhead>1800000){
		parent.$.messager.alert("失败","首部静音值超出限制",'error');
		return false;
	}
	if(vadseg==''||vadseg==undefined){
		parent.$.messager.alert("失败","尾部静音不可为空",'error');
		return false;
	}
	if(vadseg<0||vadseg>30000){
		parent.$.messager.alert("失败","尾部静音值超出限制",'error');
		return false;
	}
	if(maxsecond==''||maxsecond==undefined){
		parent.$.messager.alert("失败","最大识别时间不可为空",'error');
		return false;
	}
	if(maxsecond<0||maxsecond>60){
		parent.$.messager.alert("失败","最大识别时间值超出限制",'error');
		return false;
	}
	return true;
}
function colseDlg(){
	parent.$("#dlg").window("close");
}